package autoteam.tables;

import java.util.*;
import autoteam.SearchEngine;
import db.*;

public class SearchTableModel extends AutoTeamTableModel
{
	private static final long serialVersionUID = 1L;
	
	private List<DBRecord> searchResults;
	private String[] columnNames;
	private int rowCount;
	
	private String queryType;
	// private String query;
	
	public SearchTableModel()
	{
		super();
		searchResults = new ArrayList<DBRecord>();
		rowCount = 0;
		columnNames = new String[1];
		columnNames[0] = "Search Results";
	}
	
	public int getColumnCount()
	{
		return columnNames.length;
	}
	
	public int getRowCount()
	{
		return rowCount;
	}
	
	public String getColumnName(int col)
	{
		return columnNames[col];
	}
	
	public Object getValueAt(int row, int col)
	{
		DBRecord record = searchResults.get(row);
		
		if (queryType.equals("Players"))
		{
			return record.getFieldValue("lastname") + ", " + 
					record.getFieldValue("firstname") + " (" + 
					record.getFieldValue("playerid") + ")";
		}
		else if (queryType.equals("Coaches"))
		{
			return record.getFieldValue("lastname") + ", " + 
					record.getFieldValue("firstname");
		}
		else if (queryType.equals("Teams"))
		{
			return record.getFieldValue("teamname");
		}
		else if (queryType.equals("Leagues"))
		{
			return record.getFieldValue("leaguename");
		}
		return null;
	}
	
	public void add(int j)
	{
	}
	
	public void delete(int r)
	{
	}
	
	public void findMatches(String queryType, String query)
	{
		this.queryType = queryType;
		// this.query = query;
		
		if (queryType.equals("Players") || queryType.equals("Coaches"))
			searchResults = SearchEngine.searchPeople(queryType, query);
		else if (queryType.equals("Teams"))
			searchResults = SearchEngine.searchTeam(query);
		else if (queryType.equals("Leagues"))
			searchResults = SearchEngine.searchLeague(query);
		
		rowCount = searchResults.size();
		System.out.println(rowCount + " results");
	}
	
	public String getQueryType()
	{
		return queryType;
	}
}
